<?php

namespace app\api\controller;


use app\common\model\Hetong as HetongModel;
use app\common\model\Config;

class Hetong extends Base
{

    public function __construct()
    {
        parent::__construct();
    }



    /**
     * 编辑
     * @return \think\response\Json
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            if (!isset($input['yiname'])) {
                return returnJson(202, ' 请填写乙方姓名');
            }
            if (!isset($input['idcard'])) {
                return returnJson(202, ' 请填写乙方身份证号');
            }
            if (!isset($input['qianming'])) {
                return returnJson(202, ' 请签名');
            }
            $time = time();
            $file = $this->setHetong($input['yiname'],$input['idcard'],$input['qianming']);
            $data = array(
                'uid' => $this->uid,
                'yiname' => $input['yiname'],
                'idcard' => $input['idcard'],
                'qianming' => $input['qianming'],
                'date' => $input['date'],
                'file'=>$file,
                'update_time' => $time,
                'create_time' => $time
            );
            $res = HetongModel::setInsertGetId($data);

            if ($res) {
                return returnJson(200, ' 合同签订完成');
            }
            return returnJson(202, ' 网络异常，请重试');
        }
    }

    protected function setHetong($yiname,$idcard,$qm){
        $name = './upload/hetong/' . date('YmdHis') . $yiname . '.docx';
        $hetong = Config::getValue(['name'=>['eq','hetong']],'value');
        $date = date('Y年m月d日');
        $fp = fopen($name,'w');
        fwrite($fp,"<body style='width: 596px;position: relative'>");
        fwrite($fp,"<h1 style='text-align: center'>报名合同</h1>");
        fwrite($fp,"<p>甲方：上海溪跃体育文化传播有限公司</p>");
        fwrite($fp,"<p>乙方姓名：{$yiname}</p>");
        fwrite($fp,"<p>乙方身份证号：{$idcard}</p>");
        fwrite($fp,$hetong);
        fwrite($fp,"<p style='text-align: right;vertical-align: middle;'>监护人签名：<img style='width: 50px' src='{$qm}'></p>");
        fwrite($fp,"<p style='text-align: right;'>日期：{$date}</p>");
        fwrite($fp,"<p class='com' style='text-align: right;height: 150px;line-height: 75px;background-size: 100px;'><img style='width: 50px;' src='http://xy.xiyuezuqiu.cn/yinzhang.jpg' alt=''>上海溪跃体育文化传播有限公司</p>");
        fwrite($fp,"</body>");
        fclose($fp);
        return $name;
    }



}

